<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_openid_class extends AWS_CONTROLLER
{	
	public function get_access_rule()
	{
		$rule_action['rule_type'] = 'black';
		
		$rule_action['actions'] = array();
		
		return $rule_action;
	}

	public function setup()
	{
		HTTP::no_cache_header();
	}

	function index_action()
	{
		HTTP::redirect('/account/login/');
	}
			
	function sina_action()
	{
		if (get_setting('sina_weibo_enabled') != 'Y')
		{
			HTTP::redirect('/account/login/');
		}
		
		unset($_SESSION['sina_profile']);
		unset($_SESSION['sina_token']);
		
		$oauth = new Services_Weibo_WeiboOAuth(get_setting('sina_akey'), get_setting('sina_skey'));
		
		//$_SESSION['sina_keys'] = $oauth->getRequestToken();
		//HTTP::redirect($oauth->getAuthorizeURL($_SESSION['sina_keys']['oauth_token'], false, get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'account/openid/sina_callback/'));
		HTTP::redirect($oauth->getAuthorizeURL(get_js_url('/account/openid/sina_callback/')));
	}
	
	function sina_callback_action()
	{
		if (get_setting('sina_weibo_enabled') != 'Y')
		{
			HTTP::redirect('/account/login/');
		}
				
		if ($this->is_post() AND $_SESSION['sina_profile'])
		{
			if(get_setting('invite_reg_only') == 'Y')
			{
				H::ajax_json_output(AWS_APP::RSM(null, -1, "本站只能通过邀请注册"));
			}
			
			if (trim($_POST["user_name"]) == '')
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'user_name'
				), -1, "请输入真实姓名"));
			}
			else if ($this->model('account')->check_username($_POST["user_name"]))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'user_name'
				), -1, "真实姓名已经存在"));
			}
			else if (! $this->model('account')->check_username_char($_POST["user_name"]))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'user_name'
				), -1, "真实姓名请输入 2-7 个汉字，或 4-14 个英文"));
			}
			else if ($this->model('account')->check_username_sensitive_words($_POST["user_name"]))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'user_name'
				), -1, "真实姓名中包含敏感词或系统保留字"));
			}
			
			if ($this->model('account')->check_email($_POST['email']))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'email'
				), -1, "E-Mail 已经被使用, 或格式不正确"));
			}
			
			if (strlen($_POST["password"]) < 6 OR strlen($_POST["password"]) > 16)
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'userPassword'
				), -1, "密码长度不符合规则"));
			}
			
			if (! $_POST["agreement_chk"])
			{
				H::ajax_json_output(AWS_APP::RSM(null, -1, "请选择同意用户协议中的条款"));
			}
			
			$sina_profile = $_SESSION['sina_profile'];
			
			if (get_setting('ucenter_enabled') == 'Y')
			{
				$result = $this->model('ucenter')->register($_POST['user_name'], $_POST['password'], $_POST['email'], true);
				
				if (is_array($result))
				{				
					$uid = $result['user_info']['uid'];
				}
				else
				{
					H::ajax_json_output(AWS_APP::RSM(null, -1, $result));
				}
			}
			else
			{
				$uid = $this->model('account')->user_register($_POST['user_name'], $_POST['password'], $_POST['email'], true);
			}
					
			if ($uid)
			{				
				$this->model('sina_weibo')->bind_account($sina_profile, null, $uid, $_SESSION['sina_keys']['oauth_token'], $_SESSION['sina_keys']['oauth_token_secret'], true);
				
				unset($_SESSION['sina_profile']);
				unset($_SESSION['sina_token']);
				
				H::ajax_json_output(AWS_APP::RSM(null, 1, '微博绑定成功'));	
			}
			else
			{
				H::ajax_json_output(AWS_APP::RSM(null, "-1", "与微博通信出错 (Register), 请重新登录."));	
			}	
		}
		else
		{
			//if ($_GET['oauth_verifier'] AND ($_GET['oauth_token'] == $_SESSION['sina_keys']['oauth_token'] AND !$_SESSION['sina_keys']['last_key'] AND !$_SESSION['sina_profile']) OR $_SESSION['sina_keys']['last_key']['error'])
			if ($_GET['code'] AND (!$_SESSION['sina_token'] OR $_SESSION['sina_token']['error']))
			{
				//$oauth = new Services_Weibo_WeiboOAuth(get_setting('sina_akey'), get_setting('sina_skey'), $_SESSION['sina_keys']['oauth_token'], $_SESSION['sina_keys']['oauth_token_secret']);
				//$_SESSION['sina_keys']['last_key'] = $oauth->getAccessToken($_GET['oauth_verifier']);
				
				$oauth = new Services_Weibo_WeiboOAuth(get_setting('sina_akey'), get_setting('sina_skey'));
				
				$_SESSION['sina_token'] = $oauth->getAccessToken('code', array(
					'code' => $_GET['code'],
					'redirect_uri' => get_js_url('/account/openid/sina_callback/')
				));
						
				//$client = new Services_Weibo_WeiboClient(get_setting('sina_akey'), get_setting('sina_skey'), $_SESSION['sina_keys']['last_key']['oauth_token'], $_SESSION['sina_keys']['last_key']['oauth_token_secret']);
				$client = new Services_Weibo_WeiboClient(get_setting('sina_akey'), get_setting('sina_skey'), $_SESSION['sina_token']['access_token']);
				
				//$sina_profile = $client->verify_credentials();
				
				$uid_get = $client->get_uid();
				$sina_profile = $client->show_user_by_id($uid_get['uid']);
				
				
				if ($sina_profile['error'])
				{
					H::js_pop_msg('与微博通信出错 (' . $sina_profile['error'] . '), 请重新登录.', "/account/login/");
				}
				
				$_SESSION['sina_profile'] = $sina_profile;
			}
			
			if (!$_SESSION['sina_profile'])
			{
				H::js_pop_msg('与微博通信出错, 请重新登录.', "/account/login/");
			}
			
			if ($sina_user = $this->model('sina_weibo')->get_users_sina_by_id($_SESSION['sina_profile']['id']))
			{								
				$user_info = $this->model('account')->get_users($sina_user['uid']);
				
				$this->model('account')->setcookie_login($user_info["uid"], $user_info['email'], $user_info['password'], $user_info['salt']);
				
				unset($_SESSION['sina_profile']);
				unset($_SESSION['sina_token']);
				
				if (get_setting('ucenter_enabled') == 'Y')
				{
					HTTP::redirect('/account/sync_login/');
				}
				else
				{
					HTTP::redirect('/');
				}
			}
			else
			{
				if ($this->user_id)
				{
					$this->model('sina_weibo')->bind_account($_SESSION['sina_profile'], '/', $this->user_id);
				}
				else
				{
					if (get_setting('invite_reg_only') == 'Y')
					{
						H::js_pop_msg('本站只能通过邀请注册', get_setting('base_url'));
					}
					else
					{
						$this->crumb('完善资料', '/account/login/');
						
						TPL::assign("user_name", $_SESSION['sina_profile']['screen_name']);
						
						TPL::import_css('css/login.css');
						
						TPL::output("account/openid/callback");
					}	
				}
			}
		}
	}
	
	function qq_action()
	{
		if (get_setting('qq_t_enabled') != 'Y')
		{
			HTTP::redirect('/account/login/');
		}
				
		$this->model('qq_weibo')->init(get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'account/openid/qq_callback/', get_setting('qq_app_key'), get_setting('qq_app_secret'));
	}
	
	function qq_callback_action()
	{
		if (get_setting('qq_t_enabled') != 'Y')
		{
			HTTP::redirect('/account/login/');
		}
		
		Services_Tencent_OpenSDK_Tencent_Weibo::init(get_setting('qq_app_key'), get_setting('qq_app_secret'));
		
		if (!Services_Tencent_OpenSDK_Tencent_Weibo::getAccessToken($_GET['oauth_verifier']) OR !$uinfo = Services_Tencent_OpenSDK_Tencent_Weibo::call('user/info'))
		{
			H::js_pop_msg('与微博通信出错, 请重新登录.', "/account/login/");
		}
		
		if ($_POST['email'])
		{		
			if(get_setting('invite_reg_only') == 'Y')
			{
				H::ajax_json_output(AWS_APP::RSM(null, -1, "本站只能通过邀请注册"));
			}
		
			if (trim($_POST["user_name"]) == '')
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'user_name'
				), -1, "请输入真实姓名"));
			}
			else if ($this->model('account')->check_username($_POST["user_name"]))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'user_name'
				), -1, "真实姓名已经存在"));
			}
			else if (! $this->model('account')->check_username_char($_POST["user_name"]))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'user_name'
				), -1, "真实姓名请输入 2-7 个汉字，或 4-14 个英文"));
			}
			else if ($this->model('account')->check_username_sensitive_words($_POST["user_name"]))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'user_name'
				), -1, "真实姓名中包含敏感词或系统保留字"));
			}
			
			if ($this->model('account')->check_email($_POST['email']))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'email'
				), -1, "E-Mail 已经被使用, 或格式不正确"));
			}
			
			if (strlen($_POST["password"]) < 6 OR strlen($_POST["password"]) > 16)
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'userPassword'
				), -1, "密码长度不符合规则"));
			}
			
			if (! $_POST["agreement_chk"])
			{
				H::ajax_json_output(AWS_APP::RSM(null, -1, "请选择同意用户协议中的条款"));
			}
			
			if (get_setting('ucenter_enabled') == 'Y')
			{
				$result = $this->model('ucenter')->register($_POST['user_name'], $_POST['password'], $_POST['email'], true);
				
				if (is_array($result))
				{				
					$uid = $result['user_info']['uid'];
				}
				else
				{
					H::ajax_json_output(AWS_APP::RSM(null, -1, $result));
				}
			}
			else
			{
				$uid = $this->model('account')->user_register($_POST['user_name'], $_POST['password'], $_POST['email'], true);
			}
					
			if ($uid)
			{				
				$this->model('qq_weibo')->bind_account($uinfo, null, $uid, true);
				
				H::ajax_json_output(AWS_APP::RSM(null, 1, '微博绑定成功'));
			}
			else
			{
				H::ajax_json_output(AWS_APP::RSM(null, "-1", "与微博通信出错 (Register), 请重新登录."));	
			}	
		}
		else
		{			
			if ($qq_user = $this->model('qq_weibo')->get_users_qq_by_name($uinfo["data"]["name"]))
			{								
				$user_info = $this->model('account')->get_users($qq_user['uid']);
				
				$this->model('account')->setcookie_login($user_info["uid"], $user_info['email'], $user_info['password'], $user_info['salt']);
				
				if (get_setting('ucenter_enabled') == 'Y')
				{
					HTTP::redirect('/account/sync_login/');
				}
				else
				{
					HTTP::redirect('/');
				}
			}
			else
			{
				if ($this->user_id)
				{
					$this->model('qq_weibo')->bind_account($uinfo, '/', $this->user_id);
				}
				else
				{
					if(get_setting('invite_reg_only') == 'Y')
					{
						H::js_pop_msg('本站只能通过邀请注册', get_setting('base_url'));
					}
					else
					{		
						$this->crumb('完善资料', '/account/login/');
					
						TPL::assign("user_name", $uinfo["data"]["name"]);
						
						TPL::import_css('css/login.css');
						
						TPL::output("account/openid/callback");
					}
				}
			}
		}
	}
}